import { getProductDetailInfo,getAddOrUpdateCart } from "@/api/api";
import { getUUID } from "@/utils/uuid_token.js";


const detailStore = {
  state: {
    productInfo: {},
    uuid_token: getUUID(),
  },
  mutations: {
    SETPRODUCTINFO(state, productInfo) {
      state.productInfo = productInfo;
    },
  },
  actions: {
    //获取产品详情信息
    async setProductInfo({ commit }, id) {
      let result = await getProductDetailInfo(id);
      if (result.code === 200) {
        commit("SETPRODUCTINFO", result.data);
      }
    },
    //获取产品添加到购物车中
    async addOrUpdateCart({ commit }, {id, num}) {
        let result = await getAddOrUpdateCart(id, num);
        if (result.code === 200) {
            commit("ADDORUPDATECART", result.data);
            return "添加成功";
        } else {
            return Promise.reject(new Error("添加失败"));
        }
    },
  },
  getters: {
    //路径导航信息
    categoryView(state) {
      // categoryView至少为一个空对象，否则会报错
      return state.productInfo.categoryView || {};
    },
    //产品详情信息
    skuInfo(state) {
      return state.productInfo.skuInfo || {};
    },
    //产品属性列表
    spuSaleAttrList(state) {
        return state.productInfo.spuSaleAttrList || [];
    },
  },
};
export default detailStore;